package com.uit.ecommerce.servlets;

import com.uit.ecommerce.bo.AreaBO;
import com.uit.ecommerce.bo.CityBO;
import com.uit.ecommerce.dto.AreaDTO;
import com.uit.ecommerce.dto.CityDTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ngloc_it
 */
@WebServlet(name="ManageWeatherCity", urlPatterns={"/ManageWeatherCity"})
public class ManageWeatherCity extends HttpServlet {
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession seesion = req.getSession();
        PrintWriter out = resp.getWriter();

        String function = (String)req.getParameter("function");
//        String country = (String)req.getParameter("country");
//        String city = (String) req.getParameter("city");
        if(function.equalsIgnoreCase("reloadcity")){
            try {
                String areaName = (String) req.getParameter("area");
                reloadCity(areaName, resp);                
            } catch (Exception ex) {
                Logger.getLogger(ManageWeatherCity.class.getName()).log(Level.SEVERE, null, ex);
            }
            return;
        }else{
            try {
                ArrayList<AreaDTO> listArea = getAreas();
                if(listArea != null)
                    seesion.setAttribute("listarea", listArea);
                ArrayList<CityDTO> listCity = null;
                if ((listArea != null) && (listArea.size() > 0)) {
                    listCity = getCities(listArea.get(0).getId());
                    if(listCity != null)
                        seesion.setAttribute("listcity", listCity);
                }
            } catch (Exception ex) {
                Logger.getLogger(ManageWeatherCity.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        String path = "./jsps/QuanLyThoiTietTinhThanh.jsp";
        resp.sendRedirect(path);
    }


    private ArrayList<AreaDTO> getAreas() throws Exception{
        ArrayList<AreaDTO> results = new ArrayList<AreaDTO>();
        AreaBO areaBo = new AreaBO();
        results = areaBo.getAllArea();
        return results;
    }

    private ArrayList<CityDTO> getCities(String areaId) throws Exception{
        ArrayList<CityDTO> results = new ArrayList<CityDTO>();
        CityBO cityBo = new CityBO();
        results = cityBo.GetAllCityByAreaCode(areaId);
        return results;
    }


    private void reloadCity(String areaName, HttpServletResponse resp) throws Exception{
        CityBO cityBo = new CityBO();
        ArrayList<CityDTO> listCity = cityBo.GetAllCityByAreaName(areaName);
        if(listCity != null){
            int n = listCity.size();
            PrintWriter out = resp.getWriter();
            out.println("<tr><th>Tỉnh - Thành phố</th><th>Chức năng</th></tr>");
            for(int i = 0; i < n; i++){
                out.println("<tr>" + 
                                "<td id='td1'>" +
                                    "<a href='#'>" + listCity.get(i).getName() + "</a>" +
                                "</td>" +
                                "<td>" +
                                    "<a href='../DetailCity?function=unknown&city="+listCity.get(i).getName()+"'>Cập nhật thời tiết</a>" +
                                "</td>" +
                            "</tr>");
            }
        }
    }
}
